//
//  MOGestureOpenViewController.m
//  MobileOffice
//
//  Created by 冰冰 on 2019/7/5.
//  Copyright © 2019 com.idolmedia. All rights reserved.
//

#import "MOGestureOpenViewController.h"
#import "GesturePasswordView.h"

@interface MOGestureOpenViewController ()

@property(strong,nonatomic)GesturePasswordView *gesturePasswordView;

@end

@implementation MOGestureOpenViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self creatView];
}

-(void)creatView
{
    self.view.backgroundColor = KGrayColor_ededed;
    
    UIButton *closeButton = [[UIButton alloc]initWithFrame:CGRectMake(SCREEN_WIDTH - 30 - 30, StatusBarHeight + 10, 40, 40)];
    [closeButton setImage:IMAGE(@"companyManage_close") forState:UIControlStateNormal];
    [closeButton addTarget:self action:@selector(clickCloseButton) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:closeButton];
    
    UIImageView *iconImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, kWConversions(80), SCREEN_WIDTH/3, SCREEN_WIDTH/3)];
    iconImageView.centerX = SCREEN_WIDTH/2;
    iconImageView.image = IMAGE(@"openHeader");
    [self.view addSubview:iconImageView];
    
    GesturePasswordView *gesturePasswordView = [GesturePasswordView status:GesturePasswordStatusLogin frame:CGRectMake(0, iconImageView.bottom + 20, SCREEN_WIDTH, SCREEN_WIDTH-100) onGetCorrectPswd:^ {

        [self gotoApp];
        
    } onGetIncorrectPswd:^ {

    } errorInput:^{

    }];
    [self.view addSubview:gesturePasswordView];
}


-(void)gotoApp
{
    NSDictionary *dic = [[NSUserDefaults standardUserDefaults] objectForKey:GESTUREPASSWORD];
    NSString *pas = [[dic objectForKey:@"password"] deentryptAESBase64];
    
    MOUser *user = [MOUser sharedMOUser];
    
    NSString *postUrl = [NSString stringWithFormat:@"%@/%@?authorization=%@",FINDLOCKERBYUSER_URL,user.userName,[NSString URLEncodedString:[MOUser sharedMOUser].token]];
    
    [PPNetworkHelper GET:postUrl parameters:@{} success:^(id responseObject) {
        NSLog(@"查询手势密码:%@",responseObject);
        if ([responseObject[@"enable"] integerValue]==1) {//0：关闭 1：开启
            
            NSString *pass = responseObject[@"locker"];
            if ([pass isEqualToString:pas]) {
                
                AppDelegate *appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
                [appDelegate creatTabbar];
                
            }else{
                [[ADShowMessage shareShowMessage]showTextOnly:@"密码校验失败" showInView:self.view];
            }
            
        }else{
            [[ADShowMessage shareShowMessage]showTextOnly:@"密码校验失败" showInView:self.view];
        }
    } failure:^(NSError *error) {
        [[ADShowMessage shareShowMessage]showTextOnly:@"获取密码失败" showInView:self.view];
    }];
    
}

-(void)clickCloseButton
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}


@end
